/*******************************************************************
 *
 * Copyright (C), 2021-2022, LS, All rights reserved.
 *
 * Author      : 
 * Create Time : 2020-11-02
 * Version     : v0.1
 * Description : 
 *******************************************************************/

/*----------------------------Head file----------------------------*/
#include "pwrmgr.h"
#include "app_pwr_mgr.h"
#include "bsp_hw_config.h"


/*----------------------------macro file---------------------------*/
#define APP_PWR_MGR_EN      1

#if APP_PWR_MGR_EN
#include "log.h"
#define PWR_MGR_LOG         LOG
#else
#define PWR_MGR_LOG(...)
#endif

/*----------------------------type define--------------------------*/

/*-------------------------func declarative------------------------*/

/*----------------------------var define---------------------------*/

/*-----------------------------------------------------------------*/


#if(WORK_IN_SLEEP_MODE)

static uint8_t sleep_lock_mask = 0;

void app_pwr_mgr_init(void)
{
    //Note: TFT display need MCU in no sleep mode.
    hal_pwrmgr_register(MOD_USR2, NULL, NULL);
}


void app_pwr_mgr_enter_sleep_directly(void)     //Make system go to sleep for sure.
{
    PWR_MGR_LOG("enter_sleep_directly.\n");
    sleep_lock_mask = 0;
    if(hal_pwrmgr_is_lock(MOD_USR2)) {
        PWR_MGR_LOG("enter_sleep0.\n");
        hal_pwrmgr_unlock(MOD_USR2);
    }
}


void app_pwr_mgr_enter_sleep(app_pwr_mgr_mask_t mask)
{
    sleep_lock_mask &= ~mask;
    PWR_MGR_LOG("app_pwr_mgr_enter_sleep: %d.\n", sleep_lock_mask);
    if(0 == sleep_lock_mask) {
        if(hal_pwrmgr_is_lock(MOD_USR2)) {
            hal_pwrmgr_unlock(MOD_USR2);
            PWR_MGR_LOG("enter_sleep1.\n");
        }
    }
}


void app_pwr_mgr_exit_sleep(app_pwr_mgr_mask_t mask)
{
    sleep_lock_mask |= mask;
    PWR_MGR_LOG("app_pwr_mgr_exit_sleep: %d.\n", sleep_lock_mask);
    if(false == hal_pwrmgr_is_lock(MOD_USR2)) {
        hal_pwrmgr_lock(MOD_USR2);
        PWR_MGR_LOG("exit_sleep.\n");
    }
}

#else

void app_pwr_mgr_init(void)
{
    //Note: TFT display need MCU in no sleep mode.
    hal_pwrmgr_register(MOD_USR2, NULL, NULL);
}


void app_pwr_mgr_enter_sleep(void)
{
    PWR_MGR_LOG("enter_sleep.\n");
    hal_pwrmgr_unlock(MOD_USR2);
}


void app_pwr_mgr_exit_sleep(void)
{
    PWR_MGR_LOG("exit_sleep.\n");
    hal_pwrmgr_lock(MOD_USR2);
}

#endif



